﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data.SqlClient;

namespace Sapiens.at.SharePoint.SQLM.Receiver
{
    public class SQLReceiverSync : SQLReceiver
    {
    }

    public class SQLReceiver : SPItemEventReceiver
    {
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            try
            {
                ExecuteReceiver(properties);
            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
            }
        }
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            ExecuteReceiver(properties);
        }
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            try
            {
                ExecuteReceiver(properties);
            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
            }
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            ExecuteReceiver(properties);
        }
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
            try
            {
                ExecuteReceiver(properties);
            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
            }
        }

        private static object _lockObject = new object();
        protected virtual void ExecuteReceiver(SPItemEventProperties properties)
        {
            if (true)
            {
                lock (_lockObject)
                {
                    using (SPSite site = new SPSite(properties.WebUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists[properties.ListId];
                            SPListItem item = properties.ListItemId > 0 ? list.GetItemById(properties.ListItemId) : null;
                            var receiverData = Convert.ToString(web.Lists[properties.ListId].RootFolder.Properties[properties.ReceiverData]);
                            if (!string.IsNullOrEmpty(receiverData))
                            {
                                var xmlData = XmlData.Parse(XmlData.DecodeFrom64(receiverData));
                                SQLMatch.SQLExecute(list, item, properties, xmlData);
                                if (!SQLMatch.isBeforeEvent(properties))
                                {
                                    try
                                    {
                                        SharePoint.Receiver.EventReceiverTool.DisableEventFiring();
                                        item.SystemUpdate(false);
                                    }
                                    finally
                                    {
                                        SharePoint.Receiver.EventReceiverTool.EnableEventFiring();
                                    }
                                }
                            }

                        }
                    }
                }
            }
        } 
    }
}
